package com.ktgj.other.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ktgj.other.domain.common.*;
import com.ktgj.other.mapper.EtOrderMapper;
import com.ktgj.other.utils.Http;
import org.apache.commons.codec.digest.DigestUtils;

public class TongChengUtils {

    private static String agentAccount = "de3a3a8e-449b-4b6e-863f-9f39bdb02621";
    private static String key = "ed3d317c-57f3-40b6-9c51-1c3a606894b0";

    /**
     * 获取景区列表
     * */
    public HttpTongChengInfo getSceneryListService(TongChengInfoList tongChengInfo){
        tongChengInfo.getRequestBody().setPageSize(20);
        tongChengInfo.getRequestBody().setPageIndex(1);

        String digitalSign = agentAccount;
        digitalSign = digitalSign + tongChengInfo.getRequestBody().getPageSize();
        digitalSign = digitalSign + tongChengInfo.getRequestBody().getPageIndex();
        digitalSign = digitalSign + key;
        digitalSign = DigestUtils.md5Hex(digitalSign);

        if (tongChengInfo == null){
            tongChengInfo = new TongChengInfoList();
            RequestBodyList requestBody = new RequestBodyList();
            tongChengInfo.setRequestBody(requestBody);
        }

        RequestHead requestHead = new RequestHead();
        requestHead.setAgentAccount(agentAccount);
        requestHead.setDigitalSign(digitalSign);
        tongChengInfo.setRequestHead(requestHead);

        String json = JSONObject.toJSONString(tongChengInfo);

        String url = "http://jqapi.lvcang.cn/jingqu/Services/" + HttpUrl.GetSceneryListService;

        String post = Http.Send("POST", url, json);

        HttpTongChengInfo httpTongChengInfo = new HttpTongChengInfo();
        if (post != null){
            String scenery = null;
            int sceneryList = post.indexOf("SceneryList");
            if (sceneryList != -1){
                scenery = post.substring(sceneryList + 13, sceneryList + 15);
            }

            if (post.indexOf("TotalCount") == -1 || scenery.equals("[]")){
                int respCode = post.indexOf("respCode");
                int respMsg = post.indexOf("respMsg");
                int respTime = post.indexOf("respTime");

                String code = null;
                String msg = null;
                String time = null;
                if (post.indexOf("TotalCount") == -1){
                    code = post.substring(respCode + 11, respMsg - 2);
                    msg = post.substring(respMsg + 11, respTime - 3);
                    time = post.substring(respTime + 12, post.length() - 2);
                }
                else if (scenery.equals("[]")){
                    code = post.substring(respCode + 10, respMsg - 2);
                    msg = post.substring(respMsg + 10, respTime - 3);
                    time = post.substring(respTime + 11, post.length() - 2);
                }

                httpTongChengInfo.setRespCode(Integer.parseInt(code));
                httpTongChengInfo.setRespMsg(msg);
                httpTongChengInfo.setRespTime(time);
            }
            else {
                httpTongChengInfo = JSON.parseObject(post, HttpTongChengInfo.class);
            }
            return httpTongChengInfo;
        }
        else {
            return null;
        }
    }

    /**
     * 景区变动增量拉取接口
     * */
    public HttpTongChengInfoIncrement getSceneryIncrementService(TongChengInfoIncrement tongChengInfo){
        tongChengInfo.getRequestBody().setQueryNumber(20);
        tongChengInfo.getRequestBody().setIncrementType(0);

        String digitalSign = agentAccount;
        digitalSign = digitalSign + tongChengInfo.getRequestBody().getMaxIncrementId();
        digitalSign = digitalSign + tongChengInfo.getRequestBody().getQueryNumber();
        digitalSign = digitalSign + tongChengInfo.getRequestBody().getIncrementType();
        digitalSign = digitalSign + key;
        digitalSign = DigestUtils.md5Hex(digitalSign);

        if (tongChengInfo == null){
            tongChengInfo = new TongChengInfoIncrement();
            RequestBodyIncrement requestBody = new RequestBodyIncrement();
            tongChengInfo.setRequestBody(requestBody);
        }

        RequestHead requestHead = new RequestHead();
        requestHead.setAgentAccount(agentAccount);
        requestHead.setDigitalSign(digitalSign);
        tongChengInfo.setRequestHead(requestHead);

        String json = JSONObject.toJSONString(tongChengInfo);

        String url = "http://jqapi.lvcang.cn/jingqu/Services/" + HttpUrl.GetSceneryIncrementService;

        String post = Http.Send("POST", url, json);
        HttpTongChengInfoIncrement httpTongChengInfo = JSON.parseObject(post, HttpTongChengInfoIncrement.class);
        return httpTongChengInfo;
    }

    /**
     * 下单接口
     * */
    public HttpTongChengInfoOrder createOrderService(TongChengInfoOrder tongChengInfoOrder){
        String digitalSign = agentAccount;
        digitalSign = digitalSign + tongChengInfoOrder.getRequestBody().getPriceID();

        for (String travelerMobile : tongChengInfoOrder.getRequestBody().getTravelerMobile()) {
            digitalSign = digitalSign + travelerMobile;
        }

        digitalSign = digitalSign + tongChengInfoOrder.getRequestBody().getThirdSerialId().get(0);
        digitalSign = digitalSign + key;
        digitalSign = DigestUtils.md5Hex(digitalSign);

        RequestHead requestHead = new RequestHead();
        requestHead.setDigitalSign(digitalSign);
        requestHead.setAgentAccount(agentAccount);
        tongChengInfoOrder.setRequestHead(requestHead);

        String json = JSONObject.toJSONString(tongChengInfoOrder);

        String url = "http://jqapi.lvcang.cn/jingqu/Services/" + HttpUrl.CreateOrderService;
        String post = Http.Send("POST", url, json);
        HttpTongChengInfoOrder httpTongChengInfoOrder = JSON.parseObject(post, HttpTongChengInfoOrder.class);
        return httpTongChengInfoOrder;
    }

    /**
     * 景区详情查询接口
     * */
    public HttpTongChengInfoDetail getSceneryDetailService(TongChengInfoDetail tongChengInfoDetail){
        String digitalSign = agentAccount;
        digitalSign = digitalSign + tongChengInfoDetail.getRequestBody().getSceneryId();
        digitalSign = digitalSign + key;
        digitalSign = DigestUtils.md5Hex(digitalSign);
        RequestHead requestHead = new RequestHead();
        requestHead.setAgentAccount(agentAccount);
        requestHead.setDigitalSign(digitalSign);
        tongChengInfoDetail.setRequestHead(requestHead);

        String json = JSONObject.toJSONString(tongChengInfoDetail);

        String url = "http://jqapi.lvcang.cn/jingqu/Services/" + HttpUrl.GetSceneryDetailService;
        String post = Http.Send("POST", url, json);
        HttpTongChengInfoDetail httpTongChengInfoOrder = JSON.parseObject(post, HttpTongChengInfoDetail.class);
        return httpTongChengInfoOrder;
    }

    /**
     * 取消订单接口
     * */
    public HttpTongChengInfoCancel newCreateOrderCancelService(TongChengInfoCancel tongChengInfoCancel){
        String digitalSign = agentAccount;
        digitalSign = digitalSign + tongChengInfoCancel.getRequestBody().getSerialId();
        digitalSign = digitalSign + key;
        digitalSign = DigestUtils.md5Hex(digitalSign);
        RequestHead requestHead = new RequestHead();
        requestHead.setAgentAccount(agentAccount);
        requestHead.setDigitalSign(digitalSign);
        tongChengInfoCancel.setRequestHead(requestHead);

        String json = JSONObject.toJSONString(tongChengInfoCancel);

        String url = "http://jqapi.lvcang.cn/jingqu/Services/" + HttpUrl.NewCreateOrderCancelService;
        String post = Http.Send("POST", url, json);
        HttpTongChengInfoCancel httpTongChengInfoCancel = JSON.parseObject(post, HttpTongChengInfoCancel.class);
        return httpTongChengInfoCancel;
    }

    /**
     * 退改规则信息查询接口
     * */
    public HttpTongChengInfoRule getRefundRuleService(TongChengInfoRule tongChengInfoRule){
        String digitalSign = agentAccount;
        digitalSign = digitalSign + tongChengInfoRule.getRequestBody().getProductID();
        digitalSign = digitalSign + key;
        digitalSign = DigestUtils.md5Hex(digitalSign);
        RequestHead requestHead = new RequestHead();
        requestHead.setAgentAccount(agentAccount);
        requestHead.setDigitalSign(digitalSign);
        tongChengInfoRule.setRequestHead(requestHead);

        String json = JSONObject.toJSONString(tongChengInfoRule);

        String url = "http://jqapi.lvcang.cn/jingqu/Services/" + HttpUrl.GetRefundRuleService;
        String post = Http.Send("POST", url, json);
        HttpTongChengInfoRule httpTongChengInfoRule = JSON.parseObject(post, HttpTongChengInfoRule.class);
        return httpTongChengInfoRule;
    }

    /**
     * 下单验证接口
     * */
    public HttpTongChengInfoDate createOrderValidateService(TongChengInfoDate tongChengInfoRule){
        String digitalSign = agentAccount;
        digitalSign = digitalSign + tongChengInfoRule.getRequestBody().getPriceId();
        digitalSign = digitalSign + tongChengInfoRule.getRequestBody().getTickets();
        digitalSign = digitalSign + tongChengInfoRule.getRequestBody().getTravelerMobile();
        digitalSign = digitalSign + key;
        digitalSign = DigestUtils.md5Hex(digitalSign);
        RequestHead requestHead = new RequestHead();
        requestHead.setAgentAccount(agentAccount);
        requestHead.setDigitalSign(digitalSign);
        tongChengInfoRule.setRequestHead(requestHead);

        String json = JSONObject.toJSONString(tongChengInfoRule);

        String url = "http://jqapi.lvcang.cn/jingqu/Services/" + HttpUrl.CreateOrderValidateService;
        String post = Http.Send("POST", url, json);
        HttpTongChengInfoDate httpTongChengInfoDate = JSON.parseObject(post, HttpTongChengInfoDate.class);
        return httpTongChengInfoDate;
    }
}
